#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>

#include "memalloc.h"

#define NUM 900

int main() {
	size_t id[NUM];
	int *x, i;
	int *y[NUM];
	unsigned int time, all = 0;
	struct timeval beg, end;
	gettimeofday(&beg, NULL);
	if (!ma_init(2000000, 2000000, "c.txt")) {
		perror("");
		return 0;
	}
	gettimeofday(&end, NULL);
	time = end.tv_usec - beg.tv_usec;
	printf("for ma_init: %u\n", time);

	gettimeofday(&beg, NULL);
	for (i = 0; i < NUM; ++i) {
		id[i] = ma_alloc(16384);
		if (id[i]) {
			x = (int *)ma_get(id[i]);
		}
	}
		gettimeofday(&end, NULL);
		time = end.tv_usec - beg.tv_usec;


	printf("for ma_alloc: %u\n", time);
	all = 0;
	gettimeofday(&beg, NULL);
	for (i = 0; i < NUM; ++i) {
		y[i] = (int *)malloc(16384);
	}

	gettimeofday(&end, NULL);
	time = end.tv_usec - beg.tv_usec;

	printf("for malloc: %u\n", time);

	gettimeofday(&beg, NULL);
	for (i = 0; i < NUM; ++i) {
		if (id[i])
			ma_free(id[i]);
			}
	gettimeofday(&end, NULL);
	time = end.tv_usec - beg.tv_usec;
	printf("for ma_free: %u\n", time);

	gettimeofday(&beg, NULL);
	for (i = 0; i < NUM; ++i) {
		free(y[i]);
	}
	gettimeofday(&end, NULL);
	time = end.tv_usec - beg.tv_usec;
	printf("for free: %u\n\n", time);
	all = 0;
		for (i = 0; i < NUM; ++i) {
		gettimeofday(&beg, NULL);
		id[i] = ma_alloc(16384);
		if (id[i])
			//perror("");
			x = (int *)ma_get(id[i]);
		gettimeofday(&end, NULL);
		time = end.tv_usec - beg.tv_usec;
		all += time;
	}
	printf("for ma_alloc: %u\n", all);
	all = 0;
	for (i = 0; i < NUM; ++i) {
		gettimeofday(&beg, NULL);
		y[i] = (int *)malloc(16384);
		gettimeofday(&end, NULL);
		time = end.tv_usec - beg.tv_usec;
		all += time;

			//if (y[i]) {
			//printf("ptr: %p\n", y[i]);
			//memset(y[i], 2, 16384);
		//}
	}
	printf("for malloc: %u\n", all);


	gettimeofday(&beg, NULL);
	for (i = 0; i < NUM; ++i) {
		free(y[i]);
	}
	gettimeofday(&end, NULL);
	time = end.tv_usec - beg.tv_usec;
	printf("for free: %u\n", time);
	
	gettimeofday(&beg, NULL);
	for (i = 0; i < NUM; ++i) {
		ma_free(id[i]);
	}
	gettimeofday(&end, NULL);
	time = end.tv_usec - beg.tv_usec;
	printf("for ma_free: %u\n\n", time);

all = 0;
	for (i = 0; i < NUM; ++i) {
		gettimeofday(&beg, NULL);
		id[i] = ma_alloc(16384);
		if (id[i])
			//perror("");
			x = (int *)ma_get(id[i]);
		gettimeofday(&end, NULL);
		time = end.tv_usec - beg.tv_usec;
		all += time;

	}
	printf("for ma_alloc: %u\n", all);
	all = 0;
	for (i = 0; i < NUM; ++i) {
		gettimeofday(&beg, NULL);
		y[i] = (int *)malloc(16384);
		gettimeofday(&end, NULL);
	time = end.tv_usec - beg.tv_usec;
	all += time;

			//if (y[i]) {
			//printf("ptr: %p\n", y[i]);
			//memset(y[i], 2, 16384);
		//}
	}
	printf("for malloc: %u\n", all);


	gettimeofday(&beg, NULL);
	for (i = 0; i < NUM; ++i) {
		ma_free(id[i]);
	}
	gettimeofday(&end, NULL);
	time = end.tv_usec - beg.tv_usec;
	printf("for ma_free: %u\n", time);


	gettimeofday(&beg, NULL);
	for (i = 0; i < NUM; ++i) {
		free(y[i]);
	}
	gettimeofday(&end, NULL);
	time = end.tv_usec - beg.tv_usec;
	printf("for free: %u\n\n", time);
	all = 0;
		
for (i = 0; i < NUM; ++i) {
		gettimeofday(&beg, NULL);
		id[i] = ma_alloc(16384);
		if (id[i])
			//perror("");
			x = (int *)ma_get(id[i]);
		gettimeofday(&end, NULL);
		time = end.tv_usec - beg.tv_usec;
		all += time;

	}
	printf("for ma_alloc: %u\n", all);
	all = 0;
	for (i = 0; i < NUM; ++i) {
		gettimeofday(&beg, NULL);
		y[i] = (int *)malloc(16384);
		gettimeofday(&end, NULL);
	time = end.tv_usec - beg.tv_usec;
	all += time;

			//if (y[i]) {
			//printf("ptr: %p\n", y[i]);
			//memset(y[i], 2, 16384);
		//}
	}
	printf("for malloc: %u\n", all);


	gettimeofday(&beg, NULL);
	for (i = 0; i < NUM; ++i) {
		ma_free(id[i]);
	}
	gettimeofday(&end, NULL);
	time = end.tv_usec - beg.tv_usec;
	printf("for ma_free: %u\n", time);


	gettimeofday(&beg, NULL);
	for (i = 0; i < NUM; ++i) {
		free(y[i]);
	}
	gettimeofday(&end, NULL);
	time = end.tv_usec - beg.tv_usec;
	printf("for free: %u\n\n", time);
	all = 0;
		
for (i = 0; i < NUM; ++i) {
		gettimeofday(&beg, NULL);
		id[i] = ma_alloc(16384);
		if (id[i])
			//perror("");
			x = (int *)ma_get(id[i]);
		gettimeofday(&end, NULL);
		time = end.tv_usec - beg.tv_usec;
		all += time;

	}
	printf("for ma_alloc: %u\n", all);
	all = 0;
	for (i = 0; i < NUM; ++i) {
		gettimeofday(&beg, NULL);
		y[i] = (int *)malloc(16384);
		gettimeofday(&end, NULL);
	time = end.tv_usec - beg.tv_usec;
	all += time;
	}
	printf("for malloc: %u\n", all);


	gettimeofday(&beg, NULL);
	for (i = 0; i < NUM; ++i) {
		ma_free(id[i]);
	}
	gettimeofday(&end, NULL);
	time = end.tv_usec - beg.tv_usec;
	printf("for ma_free: %u\n", time);

	gettimeofday(&beg, NULL);
	for (i = 0; i < NUM; ++i) {
		free(y[i]);
	}
	gettimeofday(&end, NULL);
	time = end.tv_usec - beg.tv_usec;
	printf("for free: %u\n\n", time);
	all = 0;

	ma_deinit();
	return 0;
}
